package base.juc.volatiledemo;

import life.T;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

// 不保证原子性
public class D1 {

    public volatile static AtomicInteger num = new AtomicInteger();
    public  static void add() {
//        num++;  不是原子操作
        num.getAndIncrement();

    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 20000; j++) {
                    add();
                }
            }).start();
        }
        while (Thread.activeCount()>2){
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName()+"  "+ num);
    }
}
